<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            * {
                padding: 0;
                margin: 0;
                list-style: none;
            }
            html,
            body {
                width: 100%;
                height: 100%;
                border: 1px solid black;
                overflow: hidden;
                box-sizing: border-box;
            }
            body {
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .login {
                width: 60%;
                height: 500px;
            }
            #oldpwd {
                width: 100%;
                height: 50px;
                border: 1px solid black;
                font-size: 23px;
            }
            #pwd {
                width: 100%;
                height: 50px;
                border: 1px solid black;
                font-size: 23px;
            }
            #newpwd {
                width: 100%;
                height: 50px;
                border: 1px solid black;
                font-size: 23px;
            }

            #btn {
                width: 65%;
                height: 30px;
                text-align: center;
                line-height: 30px;
                font-size: 25px;
            }
        </style>
    </head>
    <body>
        <div class="update">
            <button id="back">
                返回
            </button>
            <input type="password" placeholder="原密码" id="oldpwd" />
            <br />
            <br />
            <input type="password" placeholder="新密码" id="pwd" />
            <br />
            <br />
            <input type="password" placeholder="确认密码" id="newpwd" />
            <br />
            <br />
            <button id="btn">确认修改</button>
        </div>
    </body>
    <script src="./axios.js"></script>
    <script>
        btn.onclick = () => {
            if (oldpwd.value == '' || pwd.value == '' || newpwd.value == '') {
                alert('请输入');
            } else {
                if (oldpwd.value == localStorage.pwd) {
                    if (pwd.value == newpwd.value) {
                        axios
                            .post('/updatepwd', {
                                user: localStorage.user,
                                pwd: pwd.value,
                            })
                            .then(res => {
                                if (res.data.code == 1) {
                                    alert('登录失效');
                                    location.href = '/';
                                }
                            });
                    } else {
                        alert('俩次密码不一致');
                    }
                } else {
                    alert('原始密码不正确');
                }
            }
        };
        back.onclick = () => {
            location.href = '/home';
        };
    </script>
</html>
